<%@ include file="/common/taglibs.jsp" %>
<c:set var="packageName" value="idv.takeshi.software.productline.bookstore.domain.model.useraccount."/>
<c:set var="individualAccountTypeName" value="${packageName}IndividualAccount"/>
<c:set var="organizationAccountTypeName" value="${packageName}OrganizationAccount"/>
<c:set var="isCreateAction" value="${param.action == 'create'}"/>
<c:set var="isUpdateAction" value="${param.action == 'update'}"/>

<c:set var="previousPathUrl"><spring:url value="${userAccountDto.previousPath}"/></c:set>

<head>
	<title><spring:message code="accounts.maintainAccount.header.${param.action}"/></title>
	<script type="text/javascript">
	var idvAccountType = "${individualAccountTypeName}";
	var orgAccountType = "${organizationAccountTypeName}";
	
	$(document).ready(function() {
		$("#birthday").datepicker({changeYear: true, yearRange : 'c-50:c+00'});

		$("#userAccountType").change(function(){
			var optEl = $("#userAccountType option:selected");

			if(optEl.val() === idvAccountType){
				$("tr[id^=individualInfo]").show();
				$("tr[id^=organizationInfo]").hide();
			} else if(optEl.val() === orgAccountType){
				$("tr[id^=individualInfo]").hide();
				$("tr[id^=organizationInfo]").show();
			}
		});

		$("#goBackToPreviousPath").click(function(){
			location.assign("${previousPathUrl}");
		});
		
	});
	</script>
</head>
<p><spring:message code="accounts.maintainAccount.instruction.${param.action}"/></p>

<form:form commandName="userAccountDto" method="post" action="${param.action}.html" id="userForm">
<table class="detail">
<tr>
	<td colspan="2">
	<form:hidden path="id"/>
	<form:errors path="id" cssClass="fieldError"/>
	<form:hidden path="action"/>
	<form:errors path="action" cssClass="fieldError"/>
	<form:hidden path="previousPath"/>
	<form:errors path="previousPath" cssClass="fieldError"/>
	</td>
</tr>
<tr>
    <th><label for="accountName">* <spring:message code="accounts.maintainAccount.user.accountName"/>:</label></th>
    <td>
        <form:input path="accountName" id="accountName"/>
        <form:errors path="accountName" cssClass="fieldError"/>
    </td>
</tr>
<c:if test="${individualAccountTypeName == userAccountDto.userAccountType || isCreateAction}">
	<tr id="individualInfo1">
	    <th><label for="firstName">* <spring:message code="accounts.maintainAccount.user.firstName"/>:</label></th>
	    <td>
	        <form:input path="firstName" id="firstName"/>
	        <form:errors path="firstName" cssClass="fieldError"/>
	    </td>
	</tr>
	<tr id="individualInfo2">
	    <th><label for="lastName">* <spring:message code="accounts.maintainAccount.user.lastName"/>:</label></th>
	    <td>
	        <form:input path="lastName" id="lastName"/>
	        <form:errors path="lastName" cssClass="fieldError"/>
	    </td>
	</tr>
	<tr id="individualInfo3">
	    <th><label for="sex">* <spring:message code="accounts.maintainAccount.user.sex"/>:</label></th>
	    <td>
	    	<form:select path="sex">
	    		<form:option value="MALE"><spring:message code="accounts.maintainAccount.user.sex.male"/></form:option>
	    		<form:option value="FEMALE"><spring:message code="accounts.maintainAccount.user.sex.female"/></form:option>
	    	</form:select>
	        <form:errors path="sex" cssClass="fieldError"/>
	    </td>
	</tr>
	<tr id="individualInfo4">
	    <th><label for="birthday">* <spring:message code="accounts.maintainAccount.user.birthday"/>:</label></th>
	    <td>
	    	<form:input path="birthday" id="birthday"/>
	        <form:errors path="birthday" cssClass="fieldError"/>
	    </td>
	</tr>
</c:if>
<c:if test="${organizationAccountTypeName == userAccountDto.userAccountType || isCreateAction}">
	<tr id="organizationInfo1" <c:if test="${isCreateAction}">style="display:none;"</c:if>>
	    <th><label for="name">* <spring:message code="accounts.maintainAccount.user.name"/>:</label></th>
	    <td>
	    	<form:input path="name" id="name"/>
	        <form:errors path="name" cssClass="fieldError"/>
	    </td>
	</tr>
</c:if>
<c:if test="${isCreateAction}">
	<tr>
	    <th><label for="password">* <spring:message code="accounts.maintainAccount.user.password"/>:</label></th>
	    <td>
	        <form:password path="password" id="password"/>
	        <form:errors path="password" cssClass="fieldError"/>
	    </td>
	</tr>
	<tr>
	    <th><label for="confirmedPassword">* <spring:message code="accounts.maintainAccount.user.confirmedPassword"/>:</label></th>
	    <td>
	        <form:password path="confirmedPassword" id="confirmedPassword"/>
	        <form:errors path="confirmedPassword" cssClass="fieldError"/>
	    </td>
	</tr>
</c:if>
<c:if test="${isUpdateAction}">
	<tr>
	    <th><label for="newPassword"><spring:message code="accounts.maintainAccount.user.newPassword"/>:</label></th>
	    <td>
	        <form:password path="newPassword" id="newPassword"/>
	        <form:errors path="newPassword" cssClass="fieldError"/>
	    </td>
	</tr>
	<tr>
	    <th><label for="confirmedNewPassword"><spring:message code="accounts.maintainAccount.user.confirmedNewPassword"/>:</label></th>
	    <td>
	        <form:password path="confirmedNewPassword" id="confirmedNewPassword"/>
	        <form:errors path="confirmedNewPassword" cssClass="fieldError"/>
	    </td>
	</tr>
</c:if>
<tr>
    <th><label for="zipCode">* <spring:message code="accounts.maintainAccount.user.zipCode"/>:</label></th>
    <td>
        <form:input path="zipCode" id="zipCode"/>
        <form:errors path="zipCode" cssClass="fieldError"/>
    </td>
</tr>
<tr>
    <th><label for="contactAddress">* <spring:message code="accounts.maintainAccount.user.contactAddress"/>:</label></th>
    <td>
        <form:input path="contactAddress" id="contactAddress"/>
        <form:errors path="contactAddress" cssClass="fieldError"/>
    </td>
</tr>
<tr>
    <th><label for="contactPhoneNumber"><spring:message code="accounts.maintainAccount.user.contactPhoneNumber"/>:</label></th>
    <td>
        <form:input path="contactPhoneNumber" id="contactPhoneNumber"/>
        <form:errors path="contactPhoneNumber" cssClass="fieldError"/>
    </td>
</tr>
<tr>
    <th><label for="cellPhoneNumber">* <spring:message code="accounts.maintainAccount.user.cellPhoneNumber"/>:</label></th>
    <td>
        <form:input path="cellPhoneNumber" id="cellPhoneNumber"/>
        <form:errors path="cellPhoneNumber" cssClass="fieldError"/>
    </td>
</tr>

<tr>
    <th><label for="emailAddress">* <spring:message code="accounts.maintainAccount.user.emailAddress"/>:</label></th>
    <td>
        <form:input path="emailAddress" id="emailAddress"/>
        <form:errors path="emailAddress" cssClass="fieldError"/>
    </td>
</tr>
<tr>
    <th><label for="enabled">* <spring:message code="accounts.maintainAccount.user.enabled"/>:</label></th>
    <td>
        <form:checkbox path="enabled" id="enabled"/>
        <form:errors path="enabled" cssClass="fieldError"/>
    </td>
</tr>
<c:if test="${isCreateAction}">
<tr>
	<th><label for="userAccountType">* <spring:message code="accounts.maintainAccount.user.accountType"/></label></th>
	<td>
		<form:select path="userAccountType" id="userAccountType">
			<form:option value="${individualAccountTypeName}"><spring:message code="accounts.maintainAccount.user.accountType.individualAccount"/></form:option>
			<form:option value="${organizationAccountTypeName}"><spring:message code="accounts.maintainAccount.user.accountType.organizationAccount"/></form:option>
		</form:select>
		<form:errors path="userAccountType" cssClass="fieldError"/>
	</td>
</tr>
<tr>
	<th><label for="roles">* <spring:message code="accounts.maintainAccount.user.roles"/></label></th>
	<td>
		<table>
			<tr>
				<td>
				<security:authorize access="hasRole('ROLE_ADMINISTRATOR')">
					<form:checkbox path="roles" value="${packageName}Administrator" id="administrator"/>
				</security:authorize>
				<security:authorize access="hasRole('ROLE_CLERK')">
					<form:checkbox path="roles" value="${packageName}Administrator" id="administrator" disabled="true"/>
				</security:authorize>
				<label for="administrator"><spring:message code="accounts.maintainAccount.user.roles.administrator"/></label>
				</td>
				<td>
				<form:checkbox path="roles" value="${packageName}Clerk" id="clerk"/>
				<label for="clerk"><spring:message code="accounts.maintainAccount.user.roles.clerk"/></label>
				</td>
				<td>
				<form:checkbox path="roles" value="${packageName}Customer" id="customer"/>
				<label for="customer"><spring:message code="accounts.maintainAccount.user.roles.customer"/></label>
				</td>
			</tr>
			<tr>
				<td>
				<form:checkbox path="roles" value="${packageName}Publisher" id="publisher"/>
				<label for="publisher"><spring:message code="accounts.maintainAccount.user.roles.publisher"/></label>
				</td>
				<td>
				<form:checkbox path="roles" value="${packageName}Carrier" id="carrier"/>
				<label for="carrier"><spring:message code="accounts.maintainAccount.user.roles.carrier"/></label>
				</td>
				<td><form:errors path="roles" cssClass="fieldError"/></td>
			</tr>
		</table>
	</td>
</tr>
</c:if>

<c:if test="${isUpdateAction}">
<tr>
	<th><label for="userAccountType">* <spring:message code="accounts.maintainAccount.user.accountType"/></label></th>
	<td>
		<form:select path="userAccountType" id="userAccountType" disabled="true">
			<form:option value="${individualAccountTypeName}"><spring:message code="accounts.maintainAccount.user.accountType.individualAccount"/></form:option>
			<form:option value="${organizationAccountTypeName}"><spring:message code="accounts.maintainAccount.user.accountType.organizationAccount"/></form:option>
		</form:select>
		<form:hidden path="userAccountType"/>
		<form:errors path="userAccountType" cssClass="fieldError"/>
	</td>
</tr>
<tr>
	<th><label for="roles">* <spring:message code="accounts.maintainAccount.user.roles"/></label></th>
	<td>
		 
		<table>
			<tr>
				<td>
				<security:authorize access="hasRole('ROLE_ADMINISTRATOR')">
					<form:checkbox path="roles" value="${packageName}Administrator" id="administrator"/>
				</security:authorize>
				<security:authorize access="hasRole('ROLE_CLERK')">
					<form:checkbox path="roles" value="${packageName}Administrator" id="administrator" disabled="true"/>
					<c:set var="roleName" value="${packageName}Administrator"/>
					<c:if test="${sf:hasRole(userAccountDto.roles, roleName)}">
						<input type="hidden" name="roles" value="${packageName}Administrator"/>	
					</c:if>
				</security:authorize>
				<label for="administrator"><spring:message code="accounts.maintainAccount.user.roles.administrator"/></label>
				</td>
				<td>
				<form:checkbox path="roles" value="${packageName}Clerk" id="clerk"/>
				<label for="clerk"><spring:message code="accounts.maintainAccount.user.roles.clerk"/></label>
				</td>
				<td>
				<c:set var="roleName" value="${packageName}Customer"/>
				<c:choose>
					<c:when test="${sf:hasRole(userAccountDto.roles, roleName)}">
						<form:checkbox path="roles" value="${packageName}Customer" id="customer" disabled="true"/>
						<input type="hidden" name="roles" value="${packageName}Customer"/>
					</c:when>
					<c:otherwise>
						<form:checkbox path="roles" value="${packageName}Customer" id="customer"/>
					</c:otherwise>
				</c:choose>
				<label for="customer"><spring:message code="accounts.maintainAccount.user.roles.customer"/></label>
				</td>
			</tr>
			<tr>
				<td>
				<form:checkbox path="roles" value="${packageName}Publisher" id="publisher"/>
				<label for="publisher"><spring:message code="accounts.maintainAccount.user.roles.publisher"/></label>
				</td>
				<td>
				<form:checkbox path="roles" value="${packageName}Carrier" id="carrier"/>
				<label for="carrier"><spring:message code="accounts.maintainAccount.user.roles.carrier"/></label>
				</td>
				<td><form:errors path="roles" cssClass="fieldError"/></td>
			</tr>
		</table>
		 
	</td>
</tr>
</c:if>

<tr>
    <td></td>
    <td>
        <input type="submit" class="aButton" name="save" value="<spring:message code="accounts.maintainAccount.user.submit"/>"/>
      	<input type="reset" class="aButton" name="reset" value="<spring:message code="accounts.maintainAccount.user.reset"/>"/>
      	<input type="button" class="aButton" id="goBackToPreviousPath" value="<spring:message code="accounts.maintainAccount.user.goBackToPreviousPath"/>"/>
    </td>
</tr>
</table>
</form:form>


